package cn.bestick.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.bestick.model.DataDicType;

/**
 * @author Bestick
 *
 *         be stick...
 *
 *         2015年8月27日 下午6:59:47
 */
public class DataDicTypeDao {

	/**
	 * 获取数据字典类别列表
	 * 
	 * @param con
	 * @return List<DataDicType>
	 * @throws Exception
	 */
	public List<DataDicType> getDataDicTypeList(Connection con)
			throws Exception {
		List<DataDicType> dataDicTypeList = new ArrayList<DataDicType>();
		String sql = "select * from t_dataDicType";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while (rs.next()) {
			DataDicType dataDicType = new DataDicType();
			dataDicType.setDdTypeId(rs.getInt("ddTypeId"));
			dataDicType.setDdTypeName(rs.getString("ddTypeName"));
			dataDicType.setDdTypeDesc(rs.getString("ddTypeDesc"));
			dataDicTypeList.add(dataDicType);
		}
		return dataDicTypeList;
	}

	/**
	 * 数据字典类别添加
	 * 
	 * @param con
	 * @param dataDicType
	 * @return int
	 * @throws Exception
	 */
	public int dataDicTypeAdd(Connection con, DataDicType dataDicType)
			throws Exception {
		String sql = "insert into t_dataDicType values(null,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, dataDicType.getDdTypeName());
		pstmt.setString(2, dataDicType.getDdTypeDesc());
		return pstmt.executeUpdate();
	}

	/**
	 * 根据ddTypeId获取一个数据字典类别
	 * 
	 * @param con
	 * @param ddTypeId
	 * @return DataDicType
	 * @throws Exception
	 */
	public DataDicType getDataDicTypeById(Connection con, String ddTypeId)
			throws Exception {
		DataDicType dataDicType = null;
		String sql = "select * from t_dataDicType where ddTypeId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, Integer.parseInt(ddTypeId));
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			dataDicType = new DataDicType();
			dataDicType.setDdTypeId(rs.getInt("ddTypeId"));
			dataDicType.setDdTypeName(rs.getString("ddTypeName"));
			dataDicType.setDdTypeDesc(rs.getString("ddTypeDesc"));
		}
		return dataDicType;
	}

	/**
	 * 对现有类别进行更新操作
	 * 
	 * @param con
	 * @param dataDicType
	 * @return int
	 * @throws Exception
	 */
	public int dataDicTypeUpdate(Connection con, DataDicType dataDicType)
			throws Exception {
		String sql = "update t_dataDicType set ddTypeName=?,ddTypeDesc=? where ddTypeId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, dataDicType.getDdTypeName());
		pstmt.setString(2, dataDicType.getDdTypeDesc());
		pstmt.setInt(3, dataDicType.getDdTypeId());
		return pstmt.executeUpdate();
	}

	/**
	 * 删除编号为ddTypeId的类别
	 * 
	 * @param con
	 * @param ddTypeId
	 * @return int
	 * @throws Exception
	 */
	public int dataDicTypeDelete(Connection con, String ddTypeId)
			throws Exception {
		String sql = "delete from t_dataDicType where ddTypeId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, Integer.parseInt(ddTypeId));
		return pstmt.executeUpdate();
	}
}
